package info.stapel;

import info.vokabel.MainVokabel;

public class StapelFrame extends javax.swing.JFrame {
    static Stapel sp1,sp2;
    
    public StapelFrame() {
        initComponents();
        bnCreate1ActionPerformed(null);
        bnCreate2ActionPerformed(null);
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        pnButt = new javax.swing.JPanel();
        bnCreate1 = new javax.swing.JButton();
        bnCreate2 = new javax.swing.JButton();
        bnFlip = new javax.swing.JButton();
        tf = new javax.swing.JTextField();
        bnDel = new javax.swing.JButton();
        bnFirst = new javax.swing.JButton();
        bnInsert = new javax.swing.JButton();
        sepTitle = new javax.swing.JSeparator();
        sep = new javax.swing.JSeparator();
        pnStapel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta2 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        ta1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Stapel");
        jLabel1.setToolTipText("");

        bnCreate1.setText("Stapel 1 Erzeugen");
        bnCreate1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnCreate1ActionPerformed(evt);
            }
        });

        bnCreate2.setText("Stapel 2 Erzeugen");
        bnCreate2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnCreate2ActionPerformed(evt);
            }
        });

        bnFlip.setText("Stapel umdrehen");
        bnFlip.setToolTipText("");
        bnFlip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnFlipActionPerformed(evt);
            }
        });

        bnDel.setText("Oberstes Löschen");
        bnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnDelActionPerformed(evt);
            }
        });

        bnFirst.setText("Erstes Element");
        bnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnFirstActionPerformed(evt);
            }
        });

        bnInsert.setLabel("Einfügen");
        bnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnInsertActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnButtLayout = new javax.swing.GroupLayout(pnButt);
        pnButt.setLayout(pnButtLayout);
        pnButtLayout.setHorizontalGroup(
            pnButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnButtLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bnFlip, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnButtLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bnCreate1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bnCreate2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bnDel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bnFirst, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bnInsert, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnButtLayout.setVerticalGroup(
            pnButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnButtLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bnCreate1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bnCreate2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(bnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bnFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bnDel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bnFlip, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );

        sep.setOrientation(javax.swing.SwingConstants.VERTICAL);

        ta2.setEditable(false);
        ta2.setColumns(20);
        ta2.setRows(5);
        ta2.setEnabled(false);
        jScrollPane1.setViewportView(ta2);

        ta1.setEditable(false);
        ta1.setColumns(20);
        ta1.setLineWrap(true);
        ta1.setRows(5);
        ta1.setEnabled(false);
        jScrollPane2.setViewportView(ta1);

        javax.swing.GroupLayout pnStapelLayout = new javax.swing.GroupLayout(pnStapel);
        pnStapel.setLayout(pnStapelLayout);
        pnStapelLayout.setHorizontalGroup(
            pnStapelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnStapelLayout.createSequentialGroup()
                .addContainerGap(190, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
            .addGroup(pnStapelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnStapelLayout.createSequentialGroup()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                    .addGap(211, 211, 211)))
        );
        pnStapelLayout.setVerticalGroup(
            pnStapelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnStapelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addGap(20, 20, 20))
            .addGroup(pnStapelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnStapelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addGap(21, 21, 21)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnButt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sep, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addComponent(pnStapel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(sepTitle)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sepTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnButt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnStapel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(sep, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bnCreate1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCreate1ActionPerformed
        sp1 = new Stapel();
        ta1.setEnabled(true);
        bnCreate1.setEnabled(false);
    }//GEN-LAST:event_bnCreate1ActionPerformed

    private void bnCreate2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCreate2ActionPerformed
        sp2 = new Stapel();
        ta2.setEnabled(true);
        bnCreate2.setEnabled(false);
    }//GEN-LAST:event_bnCreate2ActionPerformed

    private void bnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDelActionPerformed
        sp1.pop();
        updateTA1(sp1);
    }//GEN-LAST:event_bnDelActionPerformed

    private void bnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnInsertActionPerformed
        String s=tf.getText();
        if("".equals(s)){return;}
        sp1.push(Integer.parseInt(s));
        updateTA1(sp1);
    }//GEN-LAST:event_bnInsertActionPerformed

    private void bnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnFirstActionPerformed
        if(sp1.isEmpty()){return;}
        tf.setText(String.valueOf(sp1.aktuellesElement()));
    }//GEN-LAST:event_bnFirstActionPerformed

    private void bnFlipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnFlipActionPerformed
       if(sp1.isEmpty()){return;}
        Stapel temp=(Stapel) sp1.clone();
       sp2=new Stapel();
        while(!temp.isEmpty()){
            sp2.push(temp.aktuellesElement());
            temp.pop();
        } 
        updateTA2(sp2);
    }//GEN-LAST:event_bnFlipActionPerformed

   
    private void updateTA1(Stapel stapel){
        ta1.setText(stapel.toString());
    }
    
    private void updateTA2(Stapel stapel){
        Stapel temp=(Stapel) stapel.clone();
        
        String s="";
        while(!temp.isEmpty()){
            s+=String.valueOf(temp.aktuellesElement()+"\n");
            temp.pop();
        }
        ta2.setText(s);
    }
    
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StapelFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnCreate1;
    private javax.swing.JButton bnCreate2;
    private javax.swing.JButton bnDel;
    private javax.swing.JButton bnFirst;
    private javax.swing.JButton bnFlip;
    private javax.swing.JButton bnInsert;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnButt;
    private javax.swing.JPanel pnStapel;
    private javax.swing.JSeparator sep;
    private javax.swing.JSeparator sepTitle;
    private javax.swing.JTextArea ta1;
    private javax.swing.JTextArea ta2;
    private javax.swing.JTextField tf;
    // End of variables declaration//GEN-END:variables
}
